package cn.iocoder.yudao.module.wine.controller.admin.supplylabel;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.supplylabel.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplylabel.SupplyLabelDO;
import cn.iocoder.yudao.module.wine.service.supplylabel.SupplyLabelService;

@Tag(name = "管理后台 - 供求标签管理")
@RestController
@RequestMapping("/wine/supply-label")
@Validated
public class SupplyLabelController {

    @Resource
    private SupplyLabelService supplyLabelService;

    @PostMapping("/create")
    @Operation(summary = "创建供求标签管理")
    @PreAuthorize("@ss.hasPermission('wine:supply-label:create')")
    public CommonResult<Integer> createSupplyLabel(@Valid @RequestBody SupplyLabelSaveReqVO createReqVO) {
        return success(supplyLabelService.createSupplyLabel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供求标签管理")
    @PreAuthorize("@ss.hasPermission('wine:supply-label:update')")
    public CommonResult<Boolean> updateSupplyLabel(@Valid @RequestBody SupplyLabelSaveReqVO updateReqVO) {
        supplyLabelService.updateSupplyLabel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供求标签管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:supply-label:delete')")
    public CommonResult<Boolean> deleteSupplyLabel(@RequestParam("id") Integer id) {
        supplyLabelService.deleteSupplyLabel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供求标签管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:supply-label:query')")
    public CommonResult<SupplyLabelRespVO> getSupplyLabel(@RequestParam("id") Integer id) {
        SupplyLabelDO supplyLabel = supplyLabelService.getSupplyLabel(id);
        return success(BeanUtils.toBean(supplyLabel, SupplyLabelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得供求标签管理分页")
    @PreAuthorize("@ss.hasPermission('wine:supply-label:query')")
    public CommonResult<PageResult<SupplyLabelRespVO>> getSupplyLabelPage(@Valid SupplyLabelPageReqVO pageReqVO) {
        PageResult<SupplyLabelDO> pageResult = supplyLabelService.getSupplyLabelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplyLabelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出供求标签管理 Excel")
    @PreAuthorize("@ss.hasPermission('wine:supply-label:export')")
    @OperateLog(type = EXPORT)
    public void exportSupplyLabelExcel(@Valid SupplyLabelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplyLabelDO> list = supplyLabelService.getSupplyLabelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "供求标签管理.xls", "数据", SupplyLabelRespVO.class,
                        BeanUtils.toBean(list, SupplyLabelRespVO.class));
    }

}